package cxydmmszl.chapter04.t056;

import java.util.Scanner;

/**
 * <li style="color: red;">Prob</li>
 * 斐波那契数列问题的递归和动态规划
 * <li style="color: red;">Desc</li>
 * 给出一个整数 n，请输出斐波那契数列的第 n 项对 1e9 + 7 取模的值。<br/>
 * 输入描述：<br/>
 * 第一行一个整数 n。<br/>
 * 输出描述：<br/>
 * 输出第 n 项对于 1e9 + 7 取模的值。<br/><br/>
 * <li style="color: red;">Link</li> CD183、CD184、CD185
 *
 * @author habitplus
 * @since 2021-08-30 14:01
 */
public class Main {
    public static final int MOD = 1000000007;

    // 超时
    private static int fibonacciNth1(int n) {
        if (n < 1) return 0;
        if (n == 1 || n == 2) return 1;
        int f1 = 1, f2 = 1, f3;
        while (n > 2) {
            f3 = (f2 + f1) % 1000000007;
            f1 = f2;
            f2 = f3;
            n--;
        }
        return f2;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long ans = fibonacciNth2(n);
        System.out.println(ans);
    }

    // O(logN)
    private static long fibonacciNth2(long n) {
        if (n < 1) return 0;
        if (n == 1 || n == 2) return 1;
        long[][] base = {{1, 1}, {1, 0}};
        long[][] res = matrixPower(base, n - 2);
        return (res[0][0] + res[0][1]) % MOD;
    }

    private static long[][] matrixPower(long[][] mat, long k) {
        long[][] res = new long[mat.length][mat[0].length];
        // 先把 res 设为单位矩阵，相当于整数中的 1
        for (int i = 0; i < res.length; i++) {
            res[i][i] = 1;
        }

        long[][] tmp = mat;
        while (k != 0) {
            if ((k & 1) != 0) {
                res = multiplyMatrix(res, tmp);
            }
            tmp = multiplyMatrix(tmp, tmp);
            k >>= 1;
        }
        return res;
    }

    // 两个矩阵的乘法
    private static long[][] multiplyMatrix(long[][] m1, long[][] m2) {
        long[][] res = new long[m1.length][m2[0].length];
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m2[0].length; j++) {
                for (int k = 0; k < m2.length; k++) {
                    res[i][j] += m1[i][k] * m2[k][j];
                    res[i][j] %= MOD;
                }
            }
        }
        return res;
    }
}
